<?php

/**
 * 订单组结构
 *
 * @author hzjsq@msn.com
 * @version 0.1b
 */
class omeauto_auto_group_item {
    /**
     * 检查通过
     */
    const __OPT_ALLOW = 0;
    /**
     * 需提示或其它
     */
    const __OPT_ALERT = 1;
    /**
     * 无法合并
     */
    const __OPT_HOLD = 2;

    /**
     * 发货单分组
     */
    static $_orderGroups = null;
    /**
     * 短信分组
     */
    static $_smsGroups = null;
    /**
     * 订单数据
     * @var Array
     */
    private $orders = array();
    /**
     * 要修正的订单状态
     * @var Array
     */
    private $orderStatus = array();
    /**
     * 检查结果
     * @var Array
     */
    private $status = array('opt' => 0, 'log' => array());

    /**
     * 析构
     *
     * @param Array $orders 订单组数据
     * @return void
     */
    function __construct($orders) {

        $this->orders = $orders;
        $this->orderNums = count($orders);
    }

    /**
     * 获取订单内容
     *
     * @param void
     * @return Array
     */
    public function & getOrders() {

        return $this->orders;
    }

    /**
     * 获取送货地址
     *
     * @param void
     * @return String
     */
    public function getShipArea() {

        foreach ($this->orders as $key => $order) {

            return $order['ship_area'];
            break;
        }
    }
    
    //获取shop_id
    public function getShopId() {
        foreach ($this->orders as $key => $order) {
            return $order['shop_id'];
            break;
        }
    }

    /**
     * 识别是否全渠道订单
     *
     * @param void
     * @return String
     */
    public function isOmnichannel() {

        foreach ($this->orders as $key => $order) {
            if($order['omnichannel'] == 1){
                return true;
            }else{
                return false;
            }
            break;
        }
    }

    /**
     * 获取订单包含的门店信息
     *
     * @param void
     * @return String
     */
    public function getStoreInfo() {
        $orderExtendObj = app::get('ome')->model("order_extend");
        foreach ($this->orders as $key => $order) {
            $storeInfo = $orderExtendObj->dump(array('order_id'=>$order['order_id']),'store_dly_type,store_bn');
            return $storeInfo;
            break;
        }
    }

    /**
     * 设置是否是门店仓
     *
     * @param void
     * @return String
     */
    public function setStoreBranch() {
        $this->isStoreBranch = true;
    }

    /**
     * 是否是门店仓
     *
     * @param void
     * @return String
     */
    public function isStoreBranch() {
        return $this->isStoreBranch;
    }

    /**
     * 设置门店的履约方式
     *
     * @param $type 类型 o2o_pickup 门店自提 o2o_ship 门店配送
     * @return String
     */
    public function setStoreDlyType($type) {
        $this->storeDlyType = $type;
    }

    /**
     * 获取门店的履约方式
     *
     * @param void
     * @return String
     */
    public function getStoreDlyType() {
        return $this->storeDlyType;
    }

    /**
     * 获取订单条数
     *
     * @param void
     * @return Integer
     */
    public function getOrderNum() {

        if (empty($this->orders)) {

            return 0;
        } else {

            return count($this->orders);
        }
    }

    /**
     * 获取指定字段的值的健的分布
     *
     * @param String $field 字段名称
     * @return array
     */
    public function getGroupByField($field) {

        $result = array();
        foreach ($this->orders as $order) {

            $result[$order[$field]][] = $order['order_id'];
        }

        return $result;
    }

    /**
     * 设置物流公司
     *
     * @param Array $corp 物流公司信息
     * @return void
     */
    public function setDlyCorp($corp) {

        $this->status['change']['dlyCorp'] = $corp;
    }
     /**
     * 得到物流公司
     *
     * @param Array $corp 物流公司信息
     * @return void
     */
    public function getDlyCorp() {

        return $this->status['change']['dlyCorp'];
    }

    /**
     * 设置仓库
     *
     * @param Integer $branchId
     * @return void
     */
    public function setBranchId($branchId) {

        $this->status['change']['branchId'] = $branchId;
    }

    /**
     * 获取已经设定的仓库编号
     *
     * @param void
     * @return Integer
     */
    public function getBranchId() {

        return $this->status['change']['branchId'];
    }

    /**
     * 设置指定订单的提示状态
     *
     * @param Integer $oId 订单ID
     * @param Integer $status 要设置的订单提示状态
     * @return void
     */
    public function setOrderStatus($oId, $status) {

        if ($oId == '*') {

            foreach ($this->orders as $oid => $order) {

                $this->setOrderStatus($oid, $status);
            }
        } else {
            if ($this->orders[$oId]['pay_status'] == 0) {

                return;
            }

            if (isset($this->orderStatus[$oId])) {

                $this->orderStatus[$oId] = $this->orderStatus[$oId] | $status;
            } else {

                $this->orderStatus[$oId] = $status;
            }
        }
    }

    /**
     * 设置指定插件的检查结果
     *
     * @param Integer $optStatus 检查结果
     * @param String $plugFix 插件名
     * @return void
     */
    public function setStatus($optStatus, $plugFix) {

        //$optStatus = intval($optStatus);
        $this->status['opt'] = $this->status['opt'] > $optStatus ? $this->status['opt'] : $optStatus;
        $this->status['log'][] = array('plug' => $plugFix, 'result' => $optStatus);
    }

    /**
     * 检查订单组内容是否有效
     *
     * @param Array $orders 订单组
     * @return Boolean
     */
    public function vaild($config) {

        $autoDelivery = $config['autoConfirm'];

        if (($this->status['opt'] == self::__OPT_ALLOW || empty($this->status['opt'])) && ($autoDelivery == '1')) {
            //增加对是否已有发货单进行判断，有就不生成，需手工操作
            foreach ($this->orders as $orderId => $items) {

                //检查是否已有发货单
                $hasDeriveryIds = app::get(omeauto_auto_combine::__ORDER_APP)->model('delivery_order')->getlist('delivery_id', array('order_id' => $items['order_id']));

                if ($hasDeriveryIds) {
                    $dIds = array();
                    foreach ($hasDeriveryIds as $item) {
                        $dIds[] = $item['delivery_id'];
                    }
                    $DeriveryIds = app::get(omeauto_auto_combine::__ORDER_APP)->model('delivery')->getList('delivery_id', array('delivery_id' => $dIds, 'disabled' => 'false', 'status|notin' => array('cancel', 'back','return_back')));
                    if (is_array($DeriveryIds) && $DeriveryIds) {

                        return false;
                    }
                }
            }
            return true;
        } else {

            return false;
        }
    }

    /**
     * 检查是否有效的缓存订单组内容
     *
     * @param Array $orders 订单组s
     * @return Boolean
     */
    public function vaildBufferGroup($bufferTime) {

        $payStatus = $this->getGroupByField('pay_status');
        $codStatus = $this->getGroupByField('is_cod');

        //多是未支付，且不为货到付款,返回 false
        if (count($payStatus) == 1 && (isset($payStatus[0]) || isset($payStatus[2]) || isset($payStatus[3])) && !isset($codStatus['true'])) {

            return false;
        }

        //检查时间，正确应为 cod 为 createtime, 非 cod 为 支付时间，目前没有支付时间,暂用 createtime 判断
        if (!isset($codStatus['true'])) {
            //款到发货，需有一个已付款订单的支付时间已大于缓冲时间
            foreach ($payStatus as $pay => $ordes) {
                if ($pay == 1 || $pay == 4 || $pay == 5) {
                    foreach ($ordes as $orderId) {
                        if ($this->orders[$orderId]['paytime'] < $bufferTime) {
                            return true;
                        }
                    }
                }
            }
        } else {
            //货到付款，需有一个订单创建时间已经过缓冲时间
            foreach ($this->orders as $order) {
                if ($order['createtime'] < $bufferTime) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 对当前结果进行处理
     *
     * @param void
     * @return boolean
     */
    public function process($config) {
        $curOpInfo = kernel::single('ome_func')->getDesktopUser();

        if ($this->vaild($config)) {
            $can_split = true;
            foreach($this->orders as $key => $order){
                //判断订单是否能自动审单 匹配商品订单自动审单，是否存在不能拆分的商品
                $orderactive = kernel::single("ome_orderactive");
                $booloative = $orderactive->getmaterial_autosplit($order['order_id']);
                if($booloative['bool'] =='false'){
                    $can_split = false;
                    break;
                }
            }
            //不能拆单的，也走自动分派
            if(!$can_split) {
                $this->dispatch($config);
                return false;
            }

            $systemUser = omeauto_auto_dispatch::getSystemUser();
            $deliveryInfoo = $this->fetchDeliveryFormat();

            $arrall = array();

            foreach ($deliveryInfoo as $key => $value) {
                $count = count($value['delivery_items']); //记录书组数
                $i = 0;
                while ($i < $count) {
                    $arrall[$i][$key]['branch_id'] = $value['branch_id'];
                    $arrall[$i][$key]['logi_id'] = $value['logi_id'];
                    $arrall[$i][$key]['delivery_group'] = $value['delivery_group'];
                    $arrall[$i][$key]['sms_group'] = $value['sms_group'];
                    $arrall[$i][$key]['consignee'] = $value['consignee'];

                    foreach ($value['delivery_items'] as $dkey => $dvalue) {
                        # code...
                        if ($i === $dkey)
                            $arrall[$i][$key]['delivery_items'][$i] = $dvalue;
                    }
                    foreach ($value['order_items'] as $dkey => $dvalue) {
                        # code...
                        if ($i === $dkey)
                            $arrall[$i][$key]['order_items'][$i] = $dvalue;
                    }
                    foreach ($value['order_objects'] as $dkey => $dvalue) {
                        # code...
                        if ($i === $dkey)
                            $arrall[$i][$key]['order_objects'][$i] = $dvalue;
                    }
                    $i += 1;
                }
            }
            $deliveryInfo = $arrall;


            $ids = array();
            $deliveryObj = app::get('ome')->model("delivery");
            $orderObj = app::get('ome')->model("orders");
            $oOperation_log = app::get('ome')->model('operation_log');
            //此处要增加判断
            $sendObj = app::get('console')->model('delivery_send');
            foreach ($deliveryInfo as $key => $deliveryInfo) {
                foreach ($deliveryInfo as $orderId => $deliveryInfo) {
                    if ($orderId && $orderId > 0) {

                        $order_items = $deliveryInfo['order_items'];
                        $order_objects = $deliveryInfo['order_objects'];
                        unset($deliveryInfo['order_items']);
                        unset($deliveryInfo['order_objects']);

                        $omaterial_basic_ext = app::get('material')->model('basic_material_ext');
                        $autosplit = $omaterial_basic_ext->dump(array('bm_id' => $order_items[$key]['product_id']), 'autosplit');
                        if ($autosplit['autosplit'] == 'true') {
                            $s = 0;
                            $nums = $order_items[$key]['number'];
                            while ($s < $nums) {
                                $order_items[$key]['number'] = 1;
                                $deliveryInfo['delivery_items'][$key]['number'] = 1;
                                //  $deliveryInfo['order_objects'][$key]['number'] = 1;
                                $deliverytype = 'auto';
                                $deliveryid = $ids[] = $deliveryObj->addDelivery($orderId, $deliveryInfo, array(), $order_items, $split_status, $deliverytype);

                                $s++;
                            }
                        } else {
                            $deliverytype = 'auto';
                            $deliveryid = $ids[] = $deliveryObj->addDelivery($orderId, $deliveryInfo, array(), $order_items, $split_status, $deliverytype);
                        }
                        // $deliveryid = $ids[] = $deliveryObj->addDelivery($orderId, $deliveryInfo,array(),$order_items);


                        if (!empty($deliveryid)) {
                            //更新订单信息
                            $sdf = array('order_id' => $orderId,
                                'process_status' => 'splited',
                                'confirm' => 'Y',
                                'dispatch_time' => time(),
                                'op_id' => $systemUser['op_id'],
                                'group_id' => $systemUser['group_id']);
                            $orderObj->save($sdf);
                            $opInfo = kernel::single('ome_func')->get_system();
                            $logMsg = "订单确认,操作员:" . $curOpInfo['op_name'];
                            $oOperation_log->write_log('order_confirm@ome', $orderId, $logMsg, NULL, $opInfo);
                            unset($logMsg);
                        }
                    }
                }
            }
            //如果物流公司是当当物流不可以合并发货单

            $dly_corp = app::get(omeauto_auto_combine::__ORDER_APP)->model('dly_corp')->dump(array('corp_id' => $this->status['change']['dlyCorp']['corp_id']), 'type');
            $combine_select = app::get('ome')->getConf('ome.combine.select');

            $_isCombine = true;
            if ($dly_corp['type'] == 'DANGDANG' || $dly_corp['type'] == 'AMAZON' || $combine_select == '1') {

                $_isCombine = false;
            }
            //合并发货单
            /* if (!empty($ids) && count($ids) > 1 && $_isCombine) {
                 //多个订单合并审核，合并发货单
                 $newdly_id = $deliveryObj->merge($ids, array('logi_id' => $this->status['change']['dlyCorp']['corp_id'],'logi_name' => $this->status['change']['dlyCorp']['name']));
                 //发货单通知单推送仓库
                 ome_delivery_notice::create($newdly_id);
             }else{*/
            //发货单通知单推送仓库
            foreach ($ids as $newdly_id) {
                ome_delivery_notice::create($newdly_id);
            }

            return true;
        } else {
            $this->dispatch($config);
            return false;
        }
    }

    public function dispatch($config){
        $dispacthUser = omeauto_auto_dispatch::getAutoDispatchUser($this);

        $orderObj = app::get('ome')->model("orders");
        $oOperation_log = app::get('ome')->model('operation_log');
        foreach ($this->orders as $order) {
            if ($order['pay_status'] == '1' || $order['pay_status'] == '4' || $order['pay_status'] == '5' || $order['is_cod'] == 'true') {
                $sdf = array('order_id' => $order['order_id']);
                if (isset($this->orderStatus[$order['order_id']])) {
                    $sdf['op_id'] = $dispacthUser['op_id'];
                    $sdf['group_id'] = $dispacthUser['group_id'];
                    $sdf['confirm'] = 'N';
                    $sdf['process_status'] = 'unconfirmed';
                    $sdf['dispatch_time'] = time();
                    $sdf['auto_status'] = $this->orderStatus[$order['order_id']];
                } else {
                    $sdf['op_id'] = $dispacthUser['op_id'];
                    $sdf['group_id'] = $dispacthUser['group_id'];
                    $sdf['dispatch_time'] = time();
                    $sdf['confirm'] = 'Y';
                    $sdf['process_status'] = 'confirmed';
                }
                if ($sdf['order_id'] && $sdf['order_id'] > 0) {
                    // $orderObj->save($sdf);
                    $affect_row = $orderObj->update($sdf, array('order_id' => $sdf['order_id'], 'process_status|noequal' => 'splited'));
                    if ($affect_row) {
                        $opInfo = kernel::single('ome_func')->get_system();
                        $usersObj = app::get('desktop')->model('users');
                        $groupsObj = app::get('ome')->model('groups');
                        $confirm_opname = $usersObj->dump($dispacthUser['op_id'], 'name');
                        $confirm_opgroup = $groupsObj->dump($dispacthUser['group_id'], 'name');
                        $logMsg = "操作员:" . $curOpInfo['op_name'] . "获取订单，订单自动分派给确认组:" . $confirm_opgroup['name'] . ",确认人:" . ($confirm_opname ? $confirm_opname['name'] : '-');
                        $oOperation_log->write_log('order_dispatch@ome', $sdf['order_id'], $logMsg, NULL, $opInfo);
                        unset($logMsg);
                    }

                }
            }
        }
    }

    /**
     * 获取发货单数据格式
     *
     * @param void
     * @return Array
     */
    private function fetchDeliveryFormat($consignee = null) {

        $result = array();
        $delivery_group = $this->getDeliveryGroup();
        $sms_gorup = $this->getSendSmsGroup();
        foreach ($this->orders as $order) {
            $delivery = array('branch_id' => $this->getBranchId(),
                'logi_id' => $this->status['change']['dlyCorp']['corp_id'],
                'delivery_group' => $delivery_group,
                'sms_group' => $sms_gorup,
                'consignee' => ($consignee ? $consignee : $this->getConsignee($order)),
                'delivery_items' => array(),
                'shop_id' => $order['shop_id']
            );

            foreach ($order['objects'] as $obj) {
                foreach ($obj['items'] as $item) {
                    if ($item['delete'] == 'false') {
                        $delivery['delivery_items'][] = array(
                            'item_type' => $item['item_type'],
                            'product_id' => $item['product_id'],
                            'shop_product_id' => $item['shop_product_id'],
                            'bn' => $item['bn'],
                            'number' => $item['nums'],
                            'product_name' => $item['name'],
                            'spec_info' => $item['addon'],
                            'goods_id' =>$obj['goods_id'],
                            //'combine_hash' => md5($item['shop_product_id'] . $item['name'] . $item['addon']),
                            //'combine_hash' => md5($item['name'] . $item['addon']),
                        );

                        $delivery['order_items'][] = array(
                            'item_id' => $item['item_id'],
                            'product_id' => $item['product_id'],
                            'number' => $item['nums'],
                            'bn' => $item['bn'],
                            'product_name' => $item['name'],
                        );
                    }
                }

                if ($obj['delete'] == 'false') {
                    $delivery['order_objects'][] = array(
                        'goods_id' => $obj['goods_id'],
                        'number' => $obj['quantity'],
                    );
                }
            }
            $result[$order['order_id']] = $delivery;
        }

        return $result;
    }

    /**
     * 获取发货单分组
     */
    public function getDeliveryGroup(){

        $this->initFilters();
        foreach ((array)self::$_orderGroups as $tId => $filter) {
            if ($filter->vaild($this)) {
                return $tId;
            }
        }
        return '';
    }

    /**
     * 检查涉及仓库选择的订单分组对像是否已经存在
     *
     * @param void
     * @return void
     */
    private function initFilters() {

        if (self::$_orderGroups === null) {

            $filters = kernel::single('omeauto_auto_type')->getDeliveryGroupTypes();
            self::$_orderGroups = array();
            if ($filters) {

                foreach ($filters as $config) {

                    $filter = new omeauto_auto_group();
                    $filter->setConfig($config);
                    self::$_orderGroups[$config['tid']] = $filter;
                }
            }
        }
    }
    /**
     * 获取短信发送分组
     *
     * @param  void
     * @return void
     * @author
     **/
    public function getSendSmsGroup()
    {
        $this->initSmsFilters();
        foreach ((array)self::$_smsGroups as $tId => $filter) {
            if ($filter->vaild($this)) {
                return $tId;
            }
        }
        return '';
    }
    /**
     * 检查涉短信发送分组
     *
     * @param void
     * @return void
     */
    private function initSmsFilters() {

        if (self::$_smsGroups === null) {

            $filters = kernel::single('omeauto_auto_type')->getAutoSendSmsTypes();
            self::$_smsGroups = array();
            if ($filters) {

                foreach ($filters as $config) {

                    $filter = new omeauto_auto_group();
                    $filter->setConfig($config);
                    self::$_smsGroups[$config['tid']] = $filter;
                }
            }
        }
    }
    /**
     * 获取发货地址信息
     *
     * @param Array $order 订单数据
     * @return Array
     */
    private function getConsignee($orders) {

        return array(
            'name' => $orders['ship_name'],

            'r_time' => $orders['ship_time'],
            'mobile' => $orders['ship_mobile'],
            'zip' => $orders['ship_zip'],
            'area' => $orders['ship_area'],
            'telephone' => $orders['ship_tel'],
            'email' => $orders['ship_email'],
            'addr' => $orders['ship_addr']
        );
    }

    /**
     * 检查是否能生成发货单
     *
     * @param Void
     * @return Boolean
     */
    public function canMkDelivery() {

        foreach ($this->orders as $order) {

            #ExBOY 增加 splitting、ship_status in('0', '2')
            if($order['status'] != 'active' || $order['pause'] != 'false' || $order['abnormal'] != 'false' || !in_array($order['process_status'], array('unconfirmed','confirmed','splitting')) || !in_array($order['ship_status'], array('0', '2', '3'))){
                return false;#开启拆单_部分发货、部分退货可继续拆分 ExBOY
            }

            if ($order['is_cod'] == 'false' && !in_array($order['pay_status'], array('1', '4', '5'))) {
                return false;
            }
            
            #[拆单]检查是否还可生成发货单  chenping
            $oOrder = app::get('ome')->model('orders');
            
            $canSplit = false;
            $item_list = $oOrder->getItemBranchStore($order['order_id']);
            foreach ((array) $item_list as $il) {
                foreach ((array) $il as $var) {
                    foreach ((array) $var['order_items'] as $v) {
                        if ($v['left_nums'] > 0) {
                            $canSplit = true;
                        }
                    }
                }
            }

            if($canSplit == false) return false;
        }
        return true;
    }

    /**
     * 生成发货单位
     *
     * @param Array $consignee 收件人信息
     * @return boolean
     */
    public function mkDelivery($consignee, $deliverytype='') {

        if( $deliverytype == 'batch')
        {
            //add by ldward 20180921 因为手工批量审批，经常出现底单漏掉，特这里加一个事务锁            
            //开启事物
            kernel::database()->exec('begin');
            try
            {
                $result= $this->mkDeliveryp($consignee);
                if($result)
                {
                    // 事务提交
                    kernel::database()->exec('commit');
                }else{
                    // 事务回滚
                    kernel::database()->exec('rollback');
                }
                return $result;
            }
            catch(Exection $ex)
            {
                kernel::database()->exec('rollback');
                return false;
            }

        }else if($deliverytype == 'pickup'){
            return $this->mkDelivery1($consignee,$deliverytype);
        }else if($deliverytype == 'auto'){
            return $this->mkDelivery1($consignee,$deliverytype);
        }else if($deliverytype == 'single'){
            return $this->mkDelivery1($consignee,$deliverytype);
        }else {

            if (isset($consignee['memo'])) {
                $remark = $consignee['memo'];
                unset($consignee['memo']);
            } else {

                $remark = '';
            }
            $deliveryInfos = $this->fetchDeliveryFormat($consignee);

            $ids = array();
            $deliveryObj = app::get('ome')->model("delivery");
            $orderObj = app::get('ome')->model("orders");
            $sendObj = app::get('console')->model('delivery_send');
            //此处要增加判断
            $opInfo = kernel::single('ome_func')->getDesktopUser();
            $user_id = $opInfo['op_id'];
            $oOperation_log = app::get('ome')->model('operation_log');
            foreach ($deliveryInfos as $orderId => $deliveryInfo) {
                if ($orderId && $orderId > 0) {
                    $deliveryInfo['memo'] = $remark;

                    $order_items = $deliveryInfo['order_items'];
                    unset($deliveryInfo['order_items']);

                    #[拆单]新增$split_status是引用订单拆分后的状态
                    $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfo, array(), $order_items, $split_status);

                    if ($delivery_id) {
                        $ids[] = $delivery_id;

                        //更新订单信息
                        $sdf = array(
                            'order_id' => $orderId,
                            'process_status' => $split_status,//addDelivery()中引用值
                            'confirm' => 'Y',
                            'dispatch_time' => time(),
                            'op_id' => $user_id,
                        );
                        $orderObj->save($sdf);

                        //log日志中加入发货单号
                        $log_msg = '订单部分确认';
                        if ($split_status == 'splited') {
                            $log_msg = '订单确认';
                        }
                        $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                        $get_dly_bn = $get_dly_bn[0];
                        $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                            . $get_dly_bn['delivery_bn'] . '</a>）';

                        $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);
                    }
                }
            }

            //如果物流公司是当当物流不可以合并发货单
            $dly_corp = app::get(omeauto_auto_combine::__ORDER_APP)->model('dly_corp')->dump(array('corp_id' => $this->status['change']['dlyCorp']['corp_id']), 'type');
            $combine_select = app::get('ome')->getConf('ome.combine.select');
            $_isCombine = true;
            if ($dly_corp['type'] == 'DANGDANG' || $dly_corp['type'] == 'AMAZON' || $combine_select == '1') {
                $_isCombine = false;
            }

            //合并发货单
            if (!empty($ids) && count($ids) > 1 && $_isCombine) {
                //多个订单合并审核，合并发货单
                $newdly_id = $deliveryObj->merge($ids, array('logi_id' => $this->status['change']['dlyCorp']['corp_id'], 'logi_name' => $this->status['change']['dlyCorp']['name'], 'memo' => $remark));
                //发货单通知单推送仓库
                ome_delivery_notice::create($newdly_id);
            } else {
                //发货单通知单推送仓库
                foreach ($ids as $newdly_id) {
                    ome_delivery_notice::create($newdly_id);
                }
            }

            return true;
        }
    }

    /**
     * 生成发货单位
     *ldward
     * @param Array $consignee 收件人信息
     * @return boolean
     * false 手动审单
     */
    public function mkDelivery1($consignee, $deliverytype)
    {
        if (isset($consignee['memo'])) {
            $remark = $consignee['memo'];
            unset($consignee['memo']);
        } else {
            $remark = '';
        }
        $deliveryInfos = $this->fetchDeliveryFormat($consignee);
        $ids = array();
        $deliveryObj = app::get('ome')->model("delivery");
        $orderObj = app::get('ome')->model("orders");
        $sendObj = app::get('console')->model('delivery_send');
        //此处要增加判断
        $opInfo = kernel::single('ome_func')->getDesktopUser();
        $user_id = $opInfo['op_id'];
        $oOperation_log = app::get('ome')->model('operation_log');
        //映入查询
         $a = kernel::single('ome_orderactive');
        $is_chk =  0;
        foreach ($deliveryInfos as $orderId => $deliveryInfo) {
            $shopId = $deliveryInfo['shop_id'];
            if ($orderId && $orderId > 0) {
                $deliveryInfo['memo'] = $remark;
                $order_items = $deliveryInfo['order_items'];
                 $oim = $deliveryInfo['order_items'];
                unset($deliveryInfo['order_items']);
                #[拆单]新增$split_status部分拆分 ExBOY
                 /*处理deliveryInfo*/
                $chk = $_POST['chk'];//是否拆单
                $productsc = count($deliveryInfo['delivery_items']);  /*确认存在几个商品*/
                $activeorder = $a->getmaterial_autosplit($orderId);
                $statusw = $activeorder[$orderId];
                $deliveryInfow = $deliveryInfo;//重新接收一个数组
                $deliveryInfow['delivery_items']=null;
                $datainfo = array();
                $delivery_items_part =array();
                $order_items_part =array();
                $deliveryNums = 0;
                foreach ($deliveryInfo['delivery_items'] as $r => $row) {
                    $deliveryNums += $row['number'];
                }
                $deliveryNum = 0;
                
                foreach ($deliveryInfo['delivery_items'] as $key =>$deliveryw) {             
                    $bool = 'false'; //判断商品是否拆
                    $wbn = $a->getProduct($deliveryw['bn']);
                    foreach ($wbn as $keym => $valuem) {
                        $sales_material_bn = $valuem['sales_material_bn'];
                        if(!empty($chk[$sales_material_bn])){//
                            error_log(date('Y-m-d H:i:s').'发送数据0/'.var_export($chk[$sales_material_bn].'-----------'.$valuem['sm_id'].'---'.$deliveryw['goods_id'],true),3,DATA_DIR.'/logs/autosplititem'.date('Y-m-d').'.log');
                            if($chk[$sales_material_bn] =='on' && $valuem['sm_id']==$deliveryw['goods_id']){
                                $bool = "true";
                            }
                        }
                    } 
                    $dataw = array();
                    if($bool == "true" &&  $statusw[$deliveryw['bn']] =="true"){//商品可拆，且选择拆单按钮
                        $is_chk += 1;
                        foreach ($deliveryInfow['order_objects'] as $key1 => &$valuea) {
                            $valuea['number'] = 1;
                        }
                        unset($valuea);
                        $nums = $deliveryw['number'];
                        $i = 1;
                        $newdelivery_items = $deliveryw;
                        $orderItems = array();
                        while ( $i<= $nums) {
                            foreach ($order_items as $key2 => &$value) {
                                $value['number'] = 1;
                            }
                            unset($value);
                            $orderItems['aa'] = $order_items[$key];
                            $newdelivery_items['number'] = 1;
                            $deliveryInfow['delivery_items']['aa']=$newdelivery_items;
                            $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$orderItems, $split_status,$deliverytype);

                            $i++;
                            $ids[] = $delivery_id;
                            //更新订单信息
                            $sdf = array(
                                'order_id' => $orderId,
                                'process_status' => $split_status,//ExBOY addDelivery()中引用值
                                'confirm' => 'Y',
                                'dispatch_time' => time(),
                                'refund_status' => 0,
                                'op_id' => $user_id,
                            );
                            $orderObj->save($sdf);
                            //订单部分确认加入发货单号 ExBOY
                            $log_msg = '订单部分确认';
                            if ($split_status == 'splited') {
                                $log_msg = '订单确认';
                            }
                            $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                            $get_dly_bn = $get_dly_bn[0];
                            $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                                . $get_dly_bn['delivery_bn'] . '</a>）';

                            $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);
                        }
                    }  //this->end
                }
                if($is_chk == 0){
                    $deliveryInfow['delivery_items'] = $deliveryInfo['delivery_items'];
                    $deliveryInfow['order_objects'] = $deliveryInfo['order_objects'];
                    $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$oim, $split_status,$deliverytype);
                    if ($delivery_id) {
                        $ids[] = $delivery_id;
                        //更新订单信息
                        $sdf = array(
                            'order_id' => $orderId,
                            'process_status' => $split_status,//ExBOY addDelivery()中引用值
                            'confirm' => 'Y',
                            'dispatch_time' => time(),
                            'refund_status' => 0,
                            'op_id' => $user_id,
                        );
                        $orderObj->save($sdf);
                        //订单部分确认加入发货单号 ExBOY
                        $log_msg = '订单部分确认';
                        if ($split_status == 'splited') {
                            $log_msg = '订单确认';
                        }
                        $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                        $get_dly_bn = $get_dly_bn[0];
                        $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                            . $get_dly_bn['delivery_bn'] . '</a>）';
                        $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);
                    }
                }               
            }
        }

        //如果物流公司是当当物流不可以合并发货单
        $dly_corp = app::get(omeauto_auto_combine::__ORDER_APP)->model('dly_corp')->dump(array('corp_id' => $this->status['change']['dlyCorp']['corp_id']), 'type');
        $combine_select = app::get('ome')->getConf('ome.combine.select');
        $_isCombine = true;
        if ($dly_corp['type'] == 'DANGDANG' || $dly_corp['type'] == 'AMAZON' || $combine_select == '1') {
            $_isCombine = false;
        }

        //合并发货单
        if (!empty($ids) && count($ids) > 1 && $_isCombine) {
            //多个订单合并审核，合并发货单
            $newdly_id = $deliveryObj->merge($ids, array('logi_id' => $this->status['change']['dlyCorp']['corp_id'], 'logi_name' => $this->status['change']['dlyCorp']['name'], 'memo' => $remark));
            //发货单通知单推送仓库
            ome_delivery_notice::create($newdly_id);
        } else {
            //发货单通知单推送仓库
            foreach ($ids as $newdly_id) {
                ome_delivery_notice::create($newdly_id);
            }
        }

        return true;
    }   
    //add by ldward 2018-09-20 校验批量审单超发货数量
    public function validateQty($order_id)
    {
         $sql = "SELECT t.order_bn, t1.bn, t.nums, t1.number
                    FROM (
                        SELECT os.order_bn, ositem.bn, SUM(ositem.nums) AS nums
                        FROM sdb_ome_orders os
                            INNER JOIN sdb_ome_order_items ositem ON os.order_id = ositem.order_id
                        WHERE os.order_id ='$order_id' AND ositem.delete = 'false'
                        GROUP BY os.order_bn, ositem.bn
                    ) t
                        INNER JOIN (
                            SELECT os.order_bn, items.bn, SUM(items.number) AS number
                            FROM sdb_ome_orders os
                                INNER JOIN sdb_ome_delivery_order odo ON odo.order_id = os.order_id
                                INNER JOIN sdb_ome_delivery od ON od.delivery_id = odo.delivery_id
                                INNER JOIN sdb_wms_delivery e ON e.outer_delivery_bn = od.delivery_bn
                                INNER JOIN sdb_wms_delivery_items items ON e.delivery_id = items.delivery_id
                            WHERE os.order_id='$order_id' and e.status in (0,3)  and od.status not in('back')
                            GROUP BY os.order_bn, items.bn
                        ) t1
                        ON t.order_bn = t1.order_bn
                            AND t.bn = t1.bn
                    WHERE t.nums < t1.number ";
        $rows = kernel::database()->select($sql);
        return $rows;
    }
    /**
     * 批量审单
     *
     * @param Array $consignee 收件人信息
     * @return boolean
     */
    public function mkDeliveryp($consignee)
    {

        if (isset($consignee['memo'])) {
            $remark = $consignee['memo'];
            unset($consignee['memo']);
        } else {

            $remark = '';
        }
        $deliveryInfos = $this->fetchDeliveryFormat($consignee);

        $ids = array();
        $deliveryObj = app::get('ome')->model("delivery");
        $orderObj = app::get('ome')->model("orders");
        $sendObj = app::get('console')->model('delivery_send');
        //此处要增加判断
        $opInfo = kernel::single('ome_func')->getDesktopUser();
        $user_id = $opInfo['op_id'];
        $oOperation_log = app::get('ome')->model('operation_log');
        //映入查询

        $a = kernel::single('ome_orderactive');
        foreach ($deliveryInfos as $orderId => $deliveryInfo) {
            if ($orderId && $orderId > 0) {
                $deliveryInfo['memo'] = $remark;

                $order_items = $deliveryInfo['order_items'];
                $oim = $deliveryInfo['order_items'];
                unset($deliveryInfo['order_items']);

                /*处理deliveryInfo自动审单*/
               
                $chk = $_POST['split'];//true 开启拆单， false 未开启拆单
               
                $activeorder = $a->getmaterial_autosplit($orderId);
                $statusw = $activeorder[$orderId];

                $deliveryInfow = $deliveryInfo;//重新接收一个数组
                $deliveryInfow['delivery_items']=null;//清除发货数据

                $datainfo = array();//未使用
                foreach ($deliveryInfo['delivery_items'] as $key =>$deliveryw) 
                {                
                   
                    //开启了拆单并且当前商品可以拆分
                    if($chk == "true" &&  $statusw[$deliveryw['bn']] =="true")
                    {
                        //商品可拆，且选择拆单按钮
                        foreach ($deliveryInfow['order_objects'] as $key1 => &$valuea) {
                            $valuea['number'] = 1;
                        }
                        
                        unset($valuea);
                        $nums = $deliveryw['number'];
                        $i = 1;
                        $newdelivery_items = $deliveryw;
                        $orderItems = array();
                        while ( $i<= $nums) 
                        {
                          
                            foreach ($order_items as $key2 => &$value) 
                            {
                                $value['number'] = 1;
                            }
                            unset($value);
                            $orderItems['aa'] = $order_items[$key];

                            $newdelivery_items['number'] = 1;              
                             
                            $i++;
                            $deliveryInfow['delivery_items']['aa']=$newdelivery_items;;
                            $tmpDelivery=$this->validateQty($orderId);
                            if(!empty($tmpDelivery))
                            {
                                return false;
                            }
                            $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$orderItems, $split_status,$deliverytype);

                            $ids[] = $delivery_id;
                            //更新订单信息
                            $sdf = array(
                                'order_id' => $orderId,
                                'process_status' => $split_status,//ExBOY addDelivery()中引用值
                                'confirm' => 'Y',
                                'dispatch_time' => time(),
                                'refund_status' => 0,
                                'op_id' => $user_id,
                            );
                            $orderObj->save($sdf);

                            //订单部分确认加入发货单号 ExBOY
                            $log_msg = '订单部分确认';
                            if ($split_status == 'splited') {
                                $log_msg = '订单确认';
                            }
                            $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                            $get_dly_bn = $get_dly_bn[0];
                            $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                                . $get_dly_bn['delivery_bn'] . '</a>）';

                            $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);
                        }
                    }elseif ($chk == "true" &&  $statusw[$deliveryw['bn']] =="false") {//选择拆单，但商品不存在拆单属性

                        $fnum = $_POST['num'];//非自动拆单自动拆单数量
                        $nums = $deliveryw['number'];// 商品数量
                        $newdelivery_items = $deliveryw;
                        if($fnum < $nums){
                            //拆单
                            //$fcount = ceil($fnum / $nums); //需要创键的发货单数量
                            $endnum = $nums%$fnum;  //最后一个发货单 数量

                            $i = 1;//记录循环
                            if($endnum > 0){
                                $fcount = ceil($nums / $fnum)-1; //需要创键的发货单数量 说明有余数需要减去多余的
                                foreach ($deliveryInfow['order_objects'] as $key1 => &$valuea) {
                                    $valuea['number'] = $fnum;
                                }
                                unset($valuea);

                                while ($i <= $fcount) {
                                    //遍历发货单数据
                                    foreach ($order_items as $key2 => &$value) {
                                        $value['number'] = $fnum;
                                    }
                                    unset($value);
                                    $orderItems['aa'] = $order_items[$key];

                                    $newdelivery_items['number'] = $fnum;              
                                     
                                    $i++;
                                    $deliveryInfow['delivery_items']['aa']=$newdelivery_items;
                                    $tmpDelivery=$this->validateQty($orderId);
                                    if(!empty($tmpDelivery))
                                    {
                                        return false;
                                    }
                                    $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$orderItems, $split_status,$deliverytype);

                                    error_log(date('Y-m-d H:i:s').'发送数据2/'.var_export($orderId,true).'返回数据/'.$split_status.'-'.var_export($delivery_id,true).PHP_EOL, 3, DATA_DIR . '/logs/addDelivery' . date('Y-m-d') . '.log');

                                    $ids[] = $delivery_id;
                                    //更新订单信息
                                    $sdf = array(
                                        'order_id' => $orderId,
                                        'process_status' => $split_status,//ExBOY addDelivery()中引用值
                                        'confirm' => 'Y',
                                        'dispatch_time' => time(),
                                        'refund_status' => 0,
                                        'op_id' => $user_id,
                                    );
                                    $orderObj->save($sdf);

                                    //订单部分确认加入发货单号 ExBOY
                                    $log_msg = '订单部分确认';
                                    if ($split_status == 'splited') {
                                        $log_msg = '订单确认';
                                    }
                                    $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                                    $get_dly_bn = $get_dly_bn[0];
                                    $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                                        . $get_dly_bn['delivery_bn'] . '</a>）';

                                    $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);


                                }
                                //最后执行一次多出的数量
                                 //遍历发货单数据
                                foreach ($deliveryInfow['order_objects'] as $key1 => &$valuea) {
                                    $valuea['number'] = $endnum;
                                }unset($valuea);
                                    foreach ($order_items as $key2 => &$value) {
                                        $value['number'] = $endnum;
                                    }
                                    unset($value);
                                    $orderItems['aa'] = $order_items[$key];

                                    $newdelivery_items['number'] = $endnum;              
                                     
                                    $i++;
                                    $deliveryInfow['delivery_items']['aa']=$newdelivery_items;
                                    $tmpDelivery=$this->validateQty($orderId);
                                    if(!empty($tmpDelivery))
                                    {
                                        return false;
                                    }
                                    $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$orderItems, $split_status,$deliverytype);

                                error_log(date('Y-m-d H:i:s').'发送数据3/'.var_export($orderId,true).'返回数据/'.$split_status.'-'.var_export($delivery_id,true).PHP_EOL, 3, DATA_DIR . '/logs/addDelivery' . date('Y-m-d') . '.log');

                                    $ids[] = $delivery_id;
                                    //更新订单信息
                                    $sdf = array(
                                        'order_id' => $orderId,
                                        'process_status' => $split_status,//ExBOY addDelivery()中引用值
                                        'confirm' => 'Y',
                                        'dispatch_time' => time(),
                                        'refund_status' => 0,
                                        'op_id' => $user_id,
                                    );
                                    $orderObj->save($sdf);

                                    //订单部分确认加入发货单号 ExBOY
                                    $log_msg = '订单部分确认';
                                    if ($split_status == 'splited') {
                                        $log_msg = '订单确认';
                                    }
                                    $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                                    $get_dly_bn = $get_dly_bn[0];
                                    $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                                        . $get_dly_bn['delivery_bn'] . '</a>）';

                                    $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);


                            }else{//正好除尽
                                $fcount = ceil($nums / $fnum); //需要创键的发货单数量 说明有余数需要减去多余的
                                foreach ($deliveryInfow['order_objects'] as $key1 => &$valuea) {
                                    $valuea['number'] = $fnum;
                                }unset($valuea);

                                while ($i <= $fcount) {
                                    //遍历发货单数据
                                    foreach ($order_items as $key2 => &$value) {
                                        $value['number'] = $fnum;
                                    }
                                    unset($value);
                                    $orderItems['aa'] = $order_items[$key];

                                    $newdelivery_items['number'] = $fnum;         
                                     
                                    $i++;
                                    $deliveryInfow['delivery_items']['aa']=$newdelivery_items;
                                    $tmpDelivery=$this->validateQty($orderId);
                                    if(!empty($tmpDelivery))
                                    {
                                        return false;
                                    }
                                    $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$orderItems, $split_status,$deliverytype);
                                error_log(date('Y-m-d H:i:s').'发送数据4/'.var_export($orderId,true).'返回数据/'.$split_status.'-'.var_export($delivery_id,true).PHP_EOL, 3, DATA_DIR . '/logs/addDelivery' . date('Y-m-d') . '.log');

                                    $ids[] = $delivery_id;
                                    //更新订单信息
                                    $sdf = array(
                                        'order_id' => $orderId,
                                        'process_status' => $split_status,//ExBOY addDelivery()中引用值
                                        'confirm' => 'Y',
                                        'dispatch_time' => time(),
                                        'refund_status' => 0,
                                        'op_id' => $user_id,
                                    );
                                    $orderObj->save($sdf);

                                    //订单部分确认加入发货单号 ExBOY
                                    $log_msg = '订单部分确认';
                                    if ($split_status == 'splited') {
                                        $log_msg = '订单确认';
                                    }
                                    $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                                    $get_dly_bn = $get_dly_bn[0];
                                    $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                                        . $get_dly_bn['delivery_bn'] . '</a>）';

                                    $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);


                                }

                            }

                        }else{
                            //不拆
                             $deliveryInfow['delivery_items']['aa'] = $deliveryw;

                            $deliveryInfow['order_objects'] = $deliveryInfo['order_objects'];

                            //重新组装orderItems
                            $orderItems = array();
                            foreach ($order_items as $key => &$value) {
                                $value['number'] = 1;
                                if($deliveryw['bn'] == $value['bn'])
                                    $orderItems['aa'] = $value;
                            }
                            $tmpDelivery=$this->validateQty($orderId);
                            if(!empty($tmpDelivery))
                            {
                                return false;
                            }
                            $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$oim, $split_status,$deliverytype);
                            error_log(date('Y-m-d H:i:s').'发送数据5/'.var_export($orderId,true).'返回数据/'.$split_status.'-'.var_export($delivery_id,true).PHP_EOL, 3, DATA_DIR . '/logs/addDelivery' . date('Y-m-d') . '.log');

                            if ($delivery_id) {
                                $ids[] = $delivery_id;

                                //更新订单信息
                                $sdf = array(
                                    'order_id' => $orderId,
                                    'process_status' => $split_status,//ExBOY addDelivery()中引用值
                                    'confirm' => 'Y',
                                    'dispatch_time' => time(),
                                    'refund_status' => 0,
                                    'op_id' => $user_id,
                                );
                                $orderObj->save($sdf);
                                //订单部分确认加入发货单号 ExBOY
                                $log_msg = '订单部分确认';
                                if ($split_status == 'splited') {
                                    $log_msg = '订单确认';
                                }
                                $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                                $get_dly_bn = $get_dly_bn[0];
                                $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                                    . $get_dly_bn['delivery_bn'] . '</a>）';

                                $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);
                            }
                        }

                    }
                    else{


                        $deliveryInfow['delivery_items']['aa'] = $deliveryw;

                        $deliveryInfow['order_objects'] = $deliveryInfo['order_objects'];

                        //重新组装orderItems
                        $orderItems = array();
                                foreach ($order_items as $key => &$value) {
                                    $value['number'] = 1;
                                    if($deliveryw['bn'] == $value['bn'])
                                        $orderItems['aa'] = $value;
                                }
                        $tmpDelivery=$this->validateQty($orderId);
                        if(!empty($tmpDelivery))
                        {
                            return false;
                        }
                        $delivery_id = $deliveryObj->addDelivery($orderId, $deliveryInfow,array(),$oim, $split_status,$deliverytype);
                                error_log(date('Y-m-d H:i:s').'发送数据6/'.var_export($orderId,true).'返回数据/'.$split_status.'-'.var_export($delivery_id,true).PHP_EOL, 3, DATA_DIR . '/logs/addDelivery' . date('Y-m-d') . '.log');

                        if ($delivery_id) {
                        $ids[] = $delivery_id;

                        //更新订单信息
                        $sdf = array(
                            'order_id' => $orderId,
                            'process_status' => $split_status,//ExBOY addDelivery()中引用值
                            'confirm' => 'Y',
                            'dispatch_time' => time(),
                            'refund_status' => 0,
                            'op_id' => $user_id,
                        );
                        $orderObj->save($sdf);
                        //订单部分确认加入发货单号 ExBOY
                        $log_msg = '订单部分确认';
                        if ($split_status == 'splited') {
                            $log_msg = '订单确认';
                        }
                        $get_dly_bn = $deliveryObj->getList('delivery_id, delivery_bn', array('delivery_id' => $delivery_id), 0, 1);
                        $get_dly_bn = $get_dly_bn[0];
                        $log_msg .= '（发货单号：<a href="index.php?app=ome&ctl=admin_receipts_print&act=show_delivery_items&id=' . $delivery_id . '" target="_blank">'
                            . $get_dly_bn['delivery_bn'] . '</a>）';

                        $oOperation_log->write_log('order_confirm@ome', $orderId, $log_msg, NULL, $opInfo);

                        }
          
                    }               
                }//this->end
            }
        }

        //如果物流公司是当当物流不可以合并发货单
        $dly_corp = app::get(omeauto_auto_combine::__ORDER_APP)->model('dly_corp')->dump(array('corp_id' => $this->status['change']['dlyCorp']['corp_id']), 'type');
        $combine_select = app::get('ome')->getConf('ome.combine.select');
        $_isCombine = true;
        if ($dly_corp['type'] == 'DANGDANG' || $dly_corp['type'] == 'AMAZON' || $combine_select == '1') {
            $_isCombine = false;
        }

        //合并发货单
        if (!empty($ids) && count($ids) > 1 && $_isCombine) {
            //多个订单合并审核，合并发货单
            $newdly_id = $deliveryObj->merge($ids, array('logi_id' => $this->status['change']['dlyCorp']['corp_id'], 'logi_name' => $this->status['change']['dlyCorp']['name'], 'memo' => $remark));
            //发货单通知单推送仓库
            ome_delivery_notice::create($newdly_id);
        } else {
            //发货单通知单推送仓库
            foreach ($ids as $newdly_id) {
                ome_delivery_notice::create($newdly_id);
            }
        }

        return true;
    }

    /**
     * 获取订单重量
     *
     * @param Array $order 订单信息
     * @return
     */
     function getWeight(){

         $weight = 0;
         foreach ($this->orders as $key => $order) {
             $order_weight = app::get('ome')->model('orders')->getOrderWeight($order['order_id']);
             if ($order_weight==0){
                 $weight=0;
                 break;
             }else{
                $weight+= $order_weight;
             }

        }
        return $weight;
     }

     /**
     * 获取店铺类型
     *
     * @param void
     * @return String
     */
    public function getShopType() {

        foreach ($this->orders as $key => $order) {

            return $order['shop_type'];
            break;
        }
    }

}
